package org.ekstrabilet.web.beans;


import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import org.ekstrabilet.game.beans.*;
import org.ekstrabilet.game.logic.GameLogic;
import org.ekstrabilet.logic.exceptions.StadiumException;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.logic.StadiumLogic;
import org.ekstrabilet.ticket.beans.Ticket;
import org.ekstrabilet.ticket.logic.TicketLogic;

public class SearchBean {
	private GameBean gameToSearch =  new GameBean();
	private ArrayList<Game> games;
	private HtmlDataTable scoreTable;  
	private int selectedGameId;

    
	public int getSelectedGameId() {
		// REDIRECT TO BUY TICKETS - not index.html	
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("addTicket.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return selectedGameId;
	}

	public void setSelectedGameId(ValueChangeEvent event) {
		// SAVE IT INTO THE SESSION
		selectedGameId = ((Game) scoreTable.getRowData()).getGameID();
		
		LinkedList<Game> games = (LinkedList<Game>) GameLogic.getInstance().getGames();
		for(Game g : games)
			if(g.getGameID() == selectedGameId) {
				
				GameBean gb = new GameBean();
				gb.setTeamA(g.getTeamA());
				gb.setTeamB(g.getTeamB());
				gb.setYear(1900 + g.getGameDate().getYear());
				gb.setMonth(g.getGameDate().getMonth());
				gb.setDay(g.getGameDate().getDate());
				gb.setHour(g.getGameTime().getHours());
				gb.setMinute(g.getGameTime().getMinutes());
				
				Stadium st = null;
				try {
					st  = StadiumLogic.getInstance().getStadium(g.getStadiumName(), g.getStadiumCity());
				} catch (StadiumException e) {
				}
				
				gb.setStadium(st.getName());
				gb.setCity(st.getCity());
				gb.setCapacity(st.getCapacity());
				
				int[] tribunes = new int[4];
				for(int i = 0 ; i < 4 ; i++) {
					tribunes[i] = st.getTribune(i).getSectors().size();
				}
				gb.setTribunes(tribunes);
				
				FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("game2", g);
				FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("game", gb);
				break;
			}
	}

	public SearchBean() {
		games = new ArrayList<Game>();
		//gameToSearch.setGameDate(new Date(0, 1, 20) );
		//gameToSearch.setGameTime(new Time(8, 0, 0));
	}
		
	public GameBean getGameToSearch() {
		return gameToSearch;
	}
	public void setGameToSearch(GameBean gameToSearch) {
		this.gameToSearch = gameToSearch;
	}
	public ArrayList<Game> getGames() {
		return games;
	}
	public void setGames(ArrayList<Game> games) {
		this.games = games;
	}
	
	public String search(){
		
		games = new ArrayList<Game>(GameLogic.getInstance().searchGames(new Game(gameToSearch)));
		
		return null;
		
	}
	
	

	public HtmlDataTable getScoreTable() {
		return scoreTable;
	}

	public void setScoreTable(HtmlDataTable scoreTable) {
		this.scoreTable = scoreTable;
	}
	
	public String redirect(){
		return "addTicket.xhtml?faces-redirect=true";
	}

}
